﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RibbonEditor.Actions;

namespace RibbonEditor.Controls.Actions
{
    public partial class JavaScriptFunctionControl : UserControl, IControlRulable
    {
        private JavaScriptFunction function;

        public JavaScriptFunction Function
        {
            get { return this.function; }
            set { this.function = value; }
        }

        public JavaScriptFunctionControl()
        {
            InitializeComponent();
        }

        public JavaScriptFunctionControl(JavaScriptFunction jsFunction):this()
        {
            this.function = jsFunction;
            this.FillControls();
        }

        private void FillControls()
        {
            try
            {
                this.rcLibrary.Value = this.function.Library;
                this.strFunctionName.Value = this.function.FunctionName;

                if(function.IsReference)
                {
                    strFunctionName.Value = "isNaN";
                    strFunctionName.Enabled = false;
                    pnlHelpReference.Visible = true;
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Save()
        {
            try
            {
                if (UserControlHelper.IsControlValid(this))
                {
                    this.function.Library = this.rcLibrary.Value;
                    this.function.FunctionName = this.strFunctionName.Value;

                    UserControlHelper.CleanDirtyFields(this);
                }
                else
                {
                    MessageBox.Show(this, "Please fill all required fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void JavaScriptFunctionControl_Leave(object sender, EventArgs e)
        {
            try
            {
                if (UserControlHelper.LeaveControl(this))
                {
                    this.Save();
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IControlRulable Members


        public void BuildObjectXml(System.Xml.XmlNode parentNode)
        {
            try
            {
                this.function.AddNode(parentNode);
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
